import matplotlib.pyplot as plt
import numpy as np

# 中文显示
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei','DV Sans']
plt.rcParams['axes.unicode_minus'] = False

def intermediate_value_theorem():
    x = np.linspace(0, 2*np.pi, 1000)
    f = np.sin(x)
    
    a, b = 0.5, 4.5
    mu = 0.7  # 介于sin(0.5)和sin(4.5)之间的值
    
    plt.figure(figsize=(12, 6))
    plt.plot(x, f, 'b-', linewidth=2, label='f(x) = sin(x)')
    plt.axhline(y=mu, color='r', linestyle='--', label=f'μ = {mu}')
    
    # 标记区间端点
    plt.scatter([a, b], [np.sin(a), np.sin(b)], color='green', s=100)
    
    # 寻找介值点
    roots = []
    for i in range(len(x)-1):
        if (f[i] - mu) * (f[i+1] - mu) <= 0:
            root_x = x[i] + (x[i+1] - x[i]) * (mu - f[i]) / (f[i+1] - f[i])
            roots.append(root_x)
            plt.scatter([root_x], [mu], color='red', s=100)
    
    plt.title('介值定理演示')
    plt.xlabel('x'); plt.ylabel('f(x)')
    plt.legend(); plt.grid(True, alpha=0.3)
    plt.show()

intermediate_value_theorem()